package com.jason.randomgame;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

import com.flurry.android.FlurryAgent;
import com.jason.randomgame.mycompnents.FlashView;

public class GameCoin extends Activity implements View.OnClickListener{
	private FlashView coin;
	private final static int CHANGE_COIN = 0;
	private boolean isRolling = false;
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case CHANGE_COIN:
				int random = (int) (Math.random() * 2);
				if(random > 0){
					coin.stop(6);
				} else {
					coin.stop(0);
				}
//				start.setVisibility(View.VISIBLE);
				break;
			}
			
		};
	};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coin);
        coin = (FlashView) findViewById(R.id.coin);
        coin.initFlashBitmaps(new int[]{R.drawable.front, R.drawable.front0, R.drawable.front1, R.drawable.back2, R.drawable.back1, R.drawable.back0, R.drawable.back});
        coin.setVisibility(View.VISIBLE);
        coin.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    	FlurryAgent.logEvent("coin_game");
//    	if(isRolling){
//    		return;
//    	}
    	switch(v.getId()){
    	case R.id.coin:
    	case R.id.play:
    		if(!isRolling){
    			coin.start();
    			isRolling = true;
    		}
    		mHandler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					if(isRolling){
						mHandler.sendEmptyMessage(CHANGE_COIN);
						isRolling = false;
					} else {
					}
				}
			}, 1000);
//    		start.setVisibility(View.INVISIBLE);
//        	new Thread(){
//        		@Override
//				public void run() {
//        			try {
//        				isRolling = true;
//        				coin.start();
//        				Thread.sleep(1000);
//        				mHandler.sendEmptyMessage(CHANGE_COIN);
//        				isRolling = false;
//        			} catch (InterruptedException e) {
//        				e.printStackTrace();
//        			}
//        		};
//        	}.start();
    		break;
    		default:
    			break;
    	}

    	
    }
}
